package app.yj.channel.strategy;

import android.support.annotation.NonNull;


import java.util.ArrayList;
import java.util.List;


/**
 * Anthor:Cpb
 * Time:2019/6/29
 * Desc:地址池请求策略
 */
public class PoolStrategy {

    public static final String TYPE_HTTP_DPOOL = "dPool"; // 国内地址
    public static final String TYPE_HTTP_FPOOL = "fPool"; // 国外地址

    private int dPoolSize = 0;
    private int fPoolSize = 0;
    private IRequestCallBack requestCallBack;

    public PoolStrategy(int dPoolSize, int fPoolSize, @NonNull IRequestCallBack requestCallBack) {
        this.dPoolSize = dPoolSize;
        this.fPoolSize = fPoolSize;
        this.requestCallBack = requestCallBack;
    }

    /**
     * 状态池
     */
    private List<HttpResult> dPool = new ArrayList<>();
    private List<HttpResult> fPool = new ArrayList<>();


    //todo new
    public void setHttpPool(String type, HttpResult channelResult) {
        if (TYPE_HTTP_DPOOL.equals(type))
            dPool.add(channelResult);
        else if (TYPE_HTTP_FPOOL.equals(type))
            fPool.add(channelResult);
        doStrategy();
    }


    /**
     * 先 国内域名地址池 ---> 国外域名地址池
     */
    private synchronized void doStrategy() {
        //1.先判断 dPool ;存在就处理,不存在等待下一次处理.

        if (dPoolSize > 0) {
            if (dPool.size() <= 0) {
                return;
            } else {
                if (isAllCancel(dPool)) {  // false
                    return;
                }
                //1.1  返回成功处理,失败不做任何处理进入下一轮.
                for (HttpResult httpResult : dPool) {
                    if (httpResult.status) {
                        httpResult.callback.doCallback();
                        cancelAllHttp();
                        return;
                    }
                }

                if (dPool.size() != dPoolSize) {
                    return;
                }

            }
        }

        //2.判断 fPool ;存在就处理,不存在等待下一次处理.
        if (fPoolSize > 0) {
            if (fPool.size() <= 0) {
                return;
            } else {
                if (isAllCancel(fPool)) {
                    return;
                }
                //1.1  返回成功处理,失败不做任何处理进入下一轮.
                for (HttpResult httpResult : fPool) {
                    if (httpResult.status) {
                        httpResult.callback.doCallback();
                        cancelAllHttp();
                        return;
                    }
                }

                if (fPool.size() != fPoolSize) {
                    return;
                }
            }
        }

        if (requestCallBack != null) {
            requestCallBack.requestBlog();
        }

    }

    /**
     * 关闭所有 的请求状态.
     */
    private void cancelAllHttp() {
        if (dPool != null) {
            for (HttpResult httpResult : dPool) {
                httpResult.hasCanceled = true;
            }
        }
        if (fPool != null) {
            for (HttpResult httpResult : fPool) {
                httpResult.hasCanceled = true;
            }
        }

    }

    /**
     * 判断
     *
     * @param pool
     * @return
     */
    private boolean isAllCancel(List<HttpResult> pool) {
        for (HttpResult httpResult : pool) {
            if (httpResult.hasCanceled) {
                return true;
            }
        }
        return false;
    }

    /**
     * 结果状态类,策略用来处结果.  不能完全按,权重处理.
     */
    public static class HttpResult {
        public HttpResult(String type, boolean status, @NonNull IResultCallBack callback) {
            this.type = type;
            this.status = status;
            this.callback = callback;
        }

        public String type;    //权重类型, leancloud\bmob\blog
        public boolean status;   //请求成功,失败
        public boolean hasCanceled;   //手动关闭停止.
        public IResultCallBack callback;   //回调
    }

    public interface IResultCallBack {
        void doCallback();
    }

    public interface IRequestCallBack {
        void requestBlog();
    }

}
